package com.oracle.controller;


import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.oracle.common.FileProcess;
import com.oracle.service.*;
import com.oracle.vo.BGC;
import com.oracle.vo.Boy;
import com.oracle.vo.Girl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.Iterator;
import java.util.List;
import java.util.Map;


/**
 * @program: CampusMatching
 * @description:
 * @author: zhaolei
 * @create: 2020-07-29 11:08
 **/
@Controller
@RequestMapping("/Main")
public class MainHandler {

   @Autowired
   FeignGetBoyService feignGetBoyService;

   @Autowired
   FeignGetGirlService feignGetGirlService;

   @Autowired
   FeignRegisterService feignRegisterService;

   @Autowired
   MessageService messageService;

   @Autowired
    BGCService bgcService;

   @Autowired
   AmpqService ampqService;

    @Autowired
    FileProcess fileProcess;

    @GetMapping("/mainpage")
    public String getPage(){
        System.out.println("进入总入口");
        return "maching";
    }

    @GetMapping("/boypage")
    public String getboyPage(){
        System.out.println("进入男士入口");
        return "bpage";
    }

    @GetMapping("/girlpage")
    public String getgirlPage(){
        System.out.println("进入女士入口");
        return "gpage";
    }




    @PostMapping("/getBoy")
    public  String getboy(Girl girl,@RequestParam("file") MultipartFile file, Map<String,Object> map){
        System.out.println(girl.toString());
        //布隆过滤器过滤已经存在于缓存中的对象
        boolean flag = feignRegisterService.selectGbloom(girl);
        if(!flag){
            //图片存储在绝对路径，并返回相对路径
            String gfile=fileProcess.getVirtualPath(file);
            girl.setGpic(gfile);
            //将对象存入数据库中
            feignRegisterService.registerGirl(girl);
        }else{
            System.out.println("插入失败，数据库中存在");
        }

        List<Boy> list=(List<Boy>) feignGetBoyService.getBoy(girl);
        ObjectMapper mapper = new ObjectMapper();
        List<Boy> list1 = mapper.convertValue(list, new TypeReference<List<Boy>>() { });
        //通过rabbitmq 发送匹配成功的信息体
        Girl girl2=feignRegisterService.selectgirl(girl);
        ampqService.setAmqpAdmin();
        Iterator iterator = list1.iterator();
        while(iterator.hasNext()){
            Object object= iterator.next();
            //进行拼接
            BGC bgc=bgcService.combination((Boy) object,girl2);
            messageService.sendObject("bgc.fanout","bgc",bgc);
        }
        System.out.println(list);
        map.put("blist",list);
        return "resboy";

    }

    @PostMapping("/getGirl")
    public  String getgirl(Boy boy,@RequestParam("file") MultipartFile file, Map<String,Object> map){
        System.out.println(boy.toString());
        //布隆过滤器过滤已经存在于缓存中的对象
        Boolean flag =feignRegisterService.selectBbloom(boy);
        if(!flag){
            //图片存储在绝对路径，并返回相对路径
            String gfile=fileProcess.getVirtualPath(file);
            boy.setBpic(gfile);
            //将对象存入数据库中
            feignRegisterService.registerBoy(boy);
        }else{
            System.out.println("插入失败，数据库中存在");
        }

        List<Girl> list=(List<Girl>) feignGetGirlService.getGirl(boy);
        ObjectMapper mapper = new ObjectMapper();
        List<Girl> list1 = mapper.convertValue(list, new TypeReference<List<Girl>>() { });
        //通过rabbitmq 发送匹配成功的信息体
        Boy boy2 =feignRegisterService.selectboy(boy);
        ampqService.setAmqpAdmin();
        Iterator iterator = list1.iterator();
        while(iterator.hasNext()){
            Object object= iterator.next();
            //进行拼接
            BGC bgc=bgcService.combination(boy2,(Girl) object);
            messageService.sendObject("bgc.fanout","bgc",bgc);
        }
        System.out.println(list);
        map.put("rgirl", list);
        return "resgirl";

    }






}
